package com.leyou.item.web;

import com.leyou.item.dto.BrandDTO;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/brand")
public class BrandController {
    private final BrandService brandService;
    @Autowired
    private CategoryBrandService categoryBrandService;

    public BrandController(BrandService brandService) {
        this.brandService = brandService;
    }
    /**
     * 根据id查询品牌
     * @param id 品牌的id
     * @return 品牌对
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id")Long id){
        //BrandDTO brandDTO = brandService.queryBrandById(id);
        return ResponseEntity.ok(this.brandService.queryBrandById(id));
    }
    /**
     * 根据品牌id集合查询品牌集合
     * @param idList id集合
     * @return BrandDTO 集合
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> idList){
        List<BrandDTO> brandDTOList=brandService.queryBrandByIds(idList);
        return ResponseEntity.ok(brandDTOList);
    }

    /**
     * @param page 当前页码
     * @param rows 每页大小
     * @param key 查询条件
     * @return 品牌分页结果
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>>queryBrandByPage(
       @RequestParam(value = "page", defaultValue = "1")Integer page,
       @RequestParam(value = "rows", defaultValue = "5")Integer rows,
       @RequestParam(value = "key", required = false)String key
    ){
        return ResponseEntity.ok(brandService.queryBrandByPage(page,rows,key));
    }

    /**
     * 根据分类id查询对应的品牌集合
     *
     * @param id
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategory(
            @RequestParam("id") Long id) {

        return ResponseEntity.ok(brandService.queryBrandByCategory(id));
    }

    /**
     * 品牌新增
     * @param brandDTO
     * @return
     */
    @PostMapping()
    public ResponseEntity<Void> addBrand(
            BrandDTO brandDTO) {
        this.brandService.addBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改
     * @param brandDTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO){
        this.brandService.updateBrand(brandDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrand(@PathVariable("id") Long id){
        this.brandService.deleteBrand(id);
        return ResponseEntity.ok().build();

    }

}
